package com.sam.designpattern.builder;

import java.util.Collection;
import java.util.Map;

/**
 * @Author: Sam
 * @Date: 2022-07-25 09:43
 * @Description: 实现导出数据到文本的生成器对象
 */
public class TxtBuilder implements Builder {
    /**
     * 生成器最终构建的产品对象，相当于产品
     */
    private StringBuffer buffer = new StringBuffer();

    public StringBuffer getResult() {
        return buffer;
    }

    @Override
    public void buildHeader(ExportHeaderModel ehm) {
        buffer.append(ehm.getDeptId() + "," + ehm.getExportDate() + "\n");
    }

    @Override
    public void buildBody(Map<String, Collection<ExportDataModel>> mapData) {
        for (String tblName : mapData.keySet()) {
            buffer.append(tblName + "\n");
            for (ExportDataModel edm : mapData.get(tblName)) {
                buffer.append(edm.getProductId() + "," + edm.getPrice() + "," + edm.getAmount() + "\n");
            }
        }
    }

    @Override
    public void buildFooter(ExportFooterModel efm) {
        buffer.append(efm.getExportUser() + "\n");
    }
}
